home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / indexed.fr_ / indexed.fr
Text File  |  1995-07-04  |  16KB  |  541 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Indexed Browser"
  5.    ClientHeight    =   2745
  6.    ClientLeft      =   1965
  7.    ClientTop       =   2055
  8.    ClientWidth     =   6420
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   3435
  19.    Left            =   1905
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   2745
  22.    ScaleWidth      =   6420
  23.    Top             =   1425
  24.    Width           =   6540
  25.    Begin VB.CommandButton cmdMove 
  26.       Caption         =   ">|"
  27.       Height          =   375
  28.       Index           =   3
  29.       Left            =   3780
  30.       TabIndex        =   9
  31.       Top             =   1980
  32.       Width           =   375
  33.    End
  34.    Begin VB.CommandButton cmdMove 
  35.       Caption         =   ">"
  36.       Height          =   375
  37.       Index           =   2
  38.       Left            =   3420
  39.       TabIndex        =   8
  40.       Top             =   1980
  41.       Width           =   375
  42.    End
  43.    Begin VB.CommandButton cmdMove 
  44.       Caption         =   "<"
  45.       Height          =   375
  46.       Index           =   1
  47.       Left            =   3060
  48.       TabIndex        =   7
  49.       Top             =   1980
  50.       Width           =   375
  51.    End
  52.    Begin VB.CommandButton cmdMove 
  53.       Caption         =   "|<"
  54.       Height          =   375
  55.       Index           =   0
  56.       Left            =   2700
  57.       TabIndex        =   6
  58.       Top             =   1980
  59.       Width           =   375
  60.    End
  61.    Begin VB.TextBox txtISBN 
  62.       DataField       =   "ISBN"
  63.       DataSource      =   "dtaTitles"
  64.       Height          =   315
  65.       Left            =   1860
  66.       MaxLength       =   13
  67.       TabIndex        =   2
  68.       Top             =   1380
  69.       Width           =   1635
  70.    End
  71.    Begin VB.TextBox txtYearPublished 
  72.       DataField       =   "Year Published"
  73.       DataSource      =   "dtaTitles"
  74.       Height          =   285
  75.       Left            =   1860
  76.       TabIndex        =   1
  77.       Top             =   900
  78.       Width           =   735
  79.    End
  80.    Begin VB.TextBox txtTitle 
  81.       DataField       =   "Title"
  82.       DataSource      =   "dtaTitles"
  83.       Height          =   555
  84.       Left            =   1860
  85.       MultiLine       =   -1  'True
  86.       TabIndex        =   0
  87.       Top             =   180
  88.       Width           =   4095
  89.    End
  90.    Begin VB.Label Label3 
  91.       AutoSize        =   -1  'True
  92.       BackColor       =   &H00C0C0C0&
  93.       Caption         =   "ISBN:"
  94.       Height          =   195
  95.       Left            =   1200
  96.       TabIndex        =   5
  97.       Top             =   1440
  98.       Width           =   510
  99.    End
  100.    Begin VB.Label Label2 
  101.       AutoSize        =   -1  'True
  102.       BackColor       =   &H00C0C0C0&
  103.       Caption         =   "Year Published:"
  104.       Height          =   195
  105.       Left            =   360
  106.       TabIndex        =   4
  107.       Top             =   960
  108.       Width           =   1350
  109.    End
  110.    Begin VB.Label Label1 
  111.       AutoSize        =   -1  'True
  112.       BackColor       =   &H00C0C0C0&
  113.       Caption         =   "Title:"
  114.       Height          =   195
  115.       Left            =   1200
  116.       TabIndex        =   3
  117.       Top             =   180
  118.       Width           =   450
  119.    End
  120.    Begin VB.Menu mnuFile 
  121.       Caption         =   "&File"
  122.       Begin VB.Menu mnuFileExit 
  123.          Caption         =   "E&xit"
  124.       End
  125.    End
  126.    Begin VB.Menu mnuEdit 
  127.       Caption         =   "&Edit"
  128.       Begin VB.Menu mnuEditUndo 
  129.          Caption         =   "&Undo"
  130.          Shortcut        =   %{BKSP}
  131.       End
  132.    End
  133.    Begin VB.Menu mnuData 
  134.       Caption         =   "&Data"
  135.       Begin VB.Menu mnuSaveRecord 
  136.          Caption         =   "&Save Record"
  137.       End
  138.       Begin VB.Menu mnuDataIndex 
  139.          Caption         =   "&Index"
  140.          Begin VB.Menu mnuDataIndexISBN 
  141.             Caption         =   "&ISBN"
  142.          End
  143.          Begin VB.Menu mnuDataIndexTitle 
  144.             Caption         =   "&Title"
  145.          End
  146.       End
  147.       Begin VB.Menu mnuDataSeek 
  148.          Caption         =   "See&k"
  149.       End
  150.    End
  151. End
  152. Attribute VB_Name = "Form1"
  153. Attribute VB_Creatable = False
  154. Attribute VB_Exposed = False
  155. Option Explicit
  156.  
  157. Private rs As Recordset
  158. Private DataChanged As Boolean
  159. Private MoveCancelled As Boolean
  160.  
  161.  
  162.  
  163. Private Sub cmdMove_Click(Index As Integer)
  164.  
  165.     ' The user clicked one of the move buttons. The button clicked is
  166.     ' passed as the Index argument. The four local Const declarations
  167.     ' represent the possible values of Index
  168.  
  169.     Const MOVE_FIRST = 0
  170.     Const MOVE_PREVIOUS = 1
  171.     Const MOVE_NEXT = 2
  172.     Const MOVE_LAST = 3
  173.     
  174.     Dim msg As String
  175.  
  176.     If DataChanged Then
  177.     
  178.         ' The data have changed, so verify that the user wants to save
  179.         ' the changes to the database.
  180.         msg = "Do you want to save the changes you've made "
  181.         msg = msg & " to the current Title?"
  182.         
  183.         Select Case MsgBox(msg, vbQuestion + vbYesNoCancel)
  184.             Case vbYes
  185.             
  186.                 ' The user wants to save.
  187.                 SaveRecord
  188.             Case vbNo
  189.             
  190.                 ' The user does not want to save, so simply do nothing
  191.                 
  192.             Case vbCancel
  193.             
  194.                 ' The user clicked Cancel, so set the flag to abort the move
  195.                 MoveCancelled = True
  196.         End Select
  197.     End If
  198.     
  199.     If Not MoveCancelled Then
  200.     
  201.         ' The move has not been cancelled, so move to the indicated record.
  202.         Select Case Index
  203.             Case MOVE_FIRST
  204.                 rs.MoveFirst
  205.             Case MOVE_PREVIOUS
  206.                 rs.MovePrevious
  207.                 
  208.                 ' If we were already on the first record, moving to the
  209.                 ' previous record put us at BOF. That's not good, so
  210.                 ' so reposition on the first record.
  211.                 If rs.BOF Then rs.MoveFirst
  212.             Case MOVE_NEXT
  213.                 rs.MoveNext
  214.  
  215.                 ' If we were already on the last record, moving to the
  216.                 ' next record put us at EOF. That's not good, so
  217.                 ' so reposition on the last record.
  218.                 If rs.EOF Then rs.MoveLast
  219.             Case MOVE_LAST
  220.                 rs.MoveLast
  221.         End Select
  222.         
  223.         ' Read the values from the new current record and display them
  224.         ' in the controls on the form.
  225.         DisplayRecord
  226.     End If
  227.  
  228. End Sub
  229. Private Sub DisplayRecord()
  230.  
  231.     ' Check each field in the recordset to make sure it's non-null.
  232.     ' If it is, display it in the corresponding control. If it is null,
  233.     ' display an empty string in the control.
  234.     If Not IsNull(rs![Title]) Then txtTitle = rs![Title] Else txtTitle = ""
  235.     If Not IsNull(rs![Year Published]) Then txtYearPublished = rs![Year Published] Else txtYearPublished = ""
  236.     If Not IsNull(rs![ISBN]) Then txtISBN = rs![ISBN] Else txtISBN = ""
  237.     
  238.     ' Clear the DataChanged flag to indicate there's no need to save the
  239.     ' record.
  240.     DataChanged = False
  241. End Sub
  242.  
  243. Private Sub SaveRecord()
  244.     Dim msg As String
  245.  
  246.     On Error GoTo SaveError
  247.  
  248.     ' Verify that each control has a legal value. If a control has an illegal
  249.     ' value, create a string explaining the problem and set the focus to the
  250.     ' control.
  251.     If txtTitle = "" Then
  252.          msg = "You must enter a title."
  253.          txtTitle.SetFocus
  254.     ElseIf txtISBN = "" Then
  255.          msg = "You must enter an ISBN."
  256.          txtISBN.SetFocus
  257.     ElseIf txtYearPublished <> "" And Not IsNumeric(txtYearPublished) Then
  258.         msg = "The Year Published must be numeric."
  259.         txtYearPublished.SetFocus
  260.     End If
  261.     
  262.     If msg = "" Then
  263.     
  264.         ' No error message was built, so the data checked out okay. Set
  265.         ' the hourglass cursor.
  266.         Screen.MousePointer = 11
  267.         
  268.         ' Copy the current record from the recordset rs into the copy buffer.
  269.         rs.Edit
  270.         
  271.             ' Update the fields in the copy buffer.
  272.             WriteRecord
  273.             
  274.         ' Write the copy buffer to the database.
  275.         rs.UPDATE
  276.         
  277.         
  278.         ' Clear the DataChanged flag to indicate there's no need to save the
  279.         ' record.
  280.         DataChanged = False
  281.         
  282.         ' Restore the cursor to the default.
  283.         Screen.MousePointer = 0
  284.     Else
  285.     
  286.         ' There's an error message, so display it.
  287.         MsgBox msg, vbExclamation
  288.         MoveCancelled = True
  289.     End If
  290.  
  291. Exit Sub
  292.  
  293. SaveError:
  294.  
  295.     ' An error was generated by Visual Basic or the Jet engine.
  296.     ' Set the cursor to the default and display the error message.
  297.     Screen.MousePointer = 0
  298.     MsgBox Err.Description
  299.     
  300. Exit Sub
  301.  
  302. End Sub
  303.  
  304. Private Sub WriteRecord()
  305.  
  306.     ' Update each field in the recordset from the value of the associated
  307.     ' control on the form.
  308.     rs![Title] = txtTitle
  309.     rs![Year Published] = txtYearPublished
  310.     rs![ISBN] = txtISBN
  311. End Sub
  312.  
  313. Private Sub Form_Load()
  314.     Dim db As DATABASE
  315.     Dim dbName As String
  316.  
  317.     On Error GoTo LoadError
  318.  
  319.   ' Get the database name and open the database.
  320.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  321.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  322.     
  323.     ' Open the recordset.
  324.     Set rs = db.OpenRecordset("Titles", dbOpenTable)
  325.     
  326.     
  327.     If rs.RecordCount > 0 Then
  328.     
  329.         ' We have at least one record, so display the values of the first
  330.         ' record in the recordset in the controls on the form.
  331.         DisplayRecord
  332.         
  333.         ' Set the current index to the default, which is the primary key.
  334.         UpdateMenuStatus "PrimaryKey"
  335.  
  336.     Else
  337.     
  338.         ' An empty recordset, so display an explanation, then terminate.
  339.         MsgBox "There are no records in the Titles table.", vbCritical
  340.         End
  341.     End If
  342. Exit Sub
  343.  
  344. LoadError:
  345.     
  346.     ' An error was generated by Visual Basic or the Jet engine.
  347.     ' Display the error message and terminate gracefully.
  348.     MsgBox Err.Description
  349. End
  350.  
  351. End Sub
  352.  
  353.  
  354. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  355.  
  356.     ' Somebody wants to close the form.
  357.  
  358.     Dim msg As String
  359.  
  360.     On Error GoTo CloseError
  361.  
  362.     If DataChanged Then
  363.     
  364.         ' The user has changed data in the current record. Ask whether
  365.         ' the user wants to save the changes.
  366.         msg = "Do you want to save changes to the current record?"
  367.         
  368.         Select Case MsgBox(msg, vbQuestion + vbYesNoCancel)
  369.             Case vbYes
  370.             
  371.                 ' The user said yes, so save the changes.
  372.                 SaveRecord
  373.                 
  374.             Case vbNo
  375.             
  376.                 ' The user said no, so do nothing.
  377.             Case vbCancel
  378.             
  379.                 ' The user clicked Cancel, so cancel the unload event.
  380.                 Cancel = True
  381.         End Select
  382.     End If
  383.  
  384. Exit Sub
  385. CloseError:
  386.     Dim errorMsg As String
  387.  
  388.     ' An error was generated by Visual Basic or the Jet engine.
  389.     ' Display the error message.
  390.     errorMsg = "Error " & Err & " (" & Error$ & ") occurred."
  391.     errorMsg = errorMsg & " RECORD HAS NOT BEEN SAVED!!"
  392.     MsgBox errorMsg, vbExclamation
  393.     
  394.     ' Set the DataChanged flag.
  395.     txtTitle.DataChanged = True
  396. Exit Sub
  397.  
  398. End Sub
  399.  
  400. Private Sub mnuEditUndo_Click()
  401.     
  402.     ' The user clicked Undo, so refresh the controls on the form with
  403.     ' the contents of the current record in the recordset.
  404.     DisplayRecord
  405. End Sub
  406.  
  407. Private Sub mnuFileExit_Click()
  408.     Unload Me
  409. End Sub
  410.  
  411. Private Sub mnuSaveRecord_Click()
  412.  
  413.     ' If the record needs to be saved, save it. Otherwise, just ignore
  414.     ' the click.
  415.     If DataChanged Then SaveRecord
  416. End Sub
  417.  
  418. Private Sub txtISBN_Change()
  419.  
  420.     ' The user has made a change, so set the DataChanged flag to true to
  421.     ' indicate that the record needs to be saved.
  422.     DataChanged = True
  423. End Sub
  424.  
  425. Private Sub txtTitle_Change()
  426.  
  427.     ' The user has made a change, so set the DataChanged flag to true to
  428.     ' indicate that the record needs to be saved.
  429.     DataChanged = True
  430. End Sub
  431.  
  432. Private Sub txtYearPublished_Change()
  433.  
  434.     ' The user has made a change, so set the DataChanged flag to true to
  435.     ' indicate that the record needs to be saved.
  436.     DataChanged = True
  437. End Sub
  438.  
  439.  
  440. Private Sub mnuDataIndexISBN_Click()
  441.     Dim db As DATABASE
  442.     Dim bkMark As Variant
  443.  
  444.     ' Mark the current position.
  445.     bkMark = rs.Bookmark
  446.     
  447.     ' The user clicked the ISBN choice on the Index pop-oup menu. Set
  448.     ' the recordset index to the primary key, which is the ISBN field.
  449.     rs.Index = "PrimaryKey"
  450.     
  451.     ' Check the ISBN choice on the menu.
  452.     UpdateMenuStatus "PrimaryKey"
  453.  
  454.     ' Reset to the marked position.
  455.     rs.Bookmark = bkMark
  456. End Sub
  457.  
  458. Private Sub mnuDataIndexTitle_Click()
  459.     Dim db As DATABASE
  460.     Dim bkMark As Variant
  461.     
  462.     ' Mark the current position.
  463.     bkMark = rs.Bookmark
  464.  
  465.     ' The user clicked the Title choice on the Index pop-oup menu. Set
  466.     ' the recordset index to the Title index.
  467.     rs.Index = "Title"
  468.     
  469.     ' Check the Title choice on the menu.
  470.     UpdateMenuStatus "Title"
  471.     
  472.     ' Reset to the marked position.
  473.     rs.Bookmark = bkMark
  474. End Sub
  475.  
  476. Private Sub mnuDataSeek_Click()
  477.     Dim seekWhat As String
  478.     Dim currentIndex As String
  479.     Dim bkMark As Variant
  480.     
  481.     ' Mark the current record.
  482.     bkMark = rs.Bookmark
  483.  
  484.     ' Find out what the currently active index is.
  485.  
  486.     currentIndex = GetCurrentIndexState()
  487.  
  488.     ' Get the value(s) from the user to be sought.
  489.  
  490.     If currentIndex = "ISBN" Then
  491.         seekWhat = InputBox$("ISBN to seek:", "Customer List")
  492.     Else
  493.         seekWhat = InputBox$("State to seek:", "Customer List")
  494.     End If
  495.  
  496.     ' Seek the requested record. The first argument to the Seek method is
  497.     ' the type of comparison; in this case, it's an equality. The remaining
  498.     ' arguments are the fields in the selected index.
  499.  
  500.     rs.Seek "=", seekWhat
  501.  
  502.     ' If the seek was successful, it points the record pointer to the first
  503.     ' record matching the criteria. In this case, just refresh the form.
  504.     ' If the seek was unsuccessful, inform the user and return to the
  505.     ' originally displayed record.
  506.  
  507.     If Not rs.NoMatch Then
  508.         DisplayRecord
  509.     Else
  510.         MsgBox "Record sought not found!", vbExclamation, "Customer List"
  511.         rs.Bookmark = bkMark
  512.     End If
  513.  
  514. End Sub
  515. Private Function GetCurrentIndexState() As String
  516.  
  517.     ' This function returns the name of the currently active index.
  518.     ' It determines the index by seeing which Index menu item is checked.
  519.  
  520.     If mnuDataIndexISBN.Checked Then
  521.         GetCurrentIndexState = "ISBN"
  522.     Else
  523.         GetCurrentIndexState = "TITLE"
  524.     End If
  525. End Function
  526.  
  527. Private Sub UpdateMenuStatus(ActiveIndex As String)
  528.  
  529.     ' This routine places a check mark beside the currently selected indexing
  530.     ' method.
  531.  
  532.     ' Check the appropriate menu item based on the ActiveIndex argument.
  533.     ' Uncheck all the others.
  534.  
  535.     mnuDataIndexISBN.Checked = IIf(ActiveIndex = "PrimaryKey", True, False)
  536.     mnuDataIndexTitle.Checked = IIf(ActiveIndex = "Title", True, False)
  537.  
  538. End Sub
  539.  
  540.  
  541.